<template>
  <div class="image-viewer" v-if="visible">
    <div class="image-viewer-overlay" @click="closeViewer"></div>
    <div class="image-viewer-content">
      <div class="image-viewer-header">
        <h3>{{ title }}</h3>
        <button @click="closeViewer" class="close-button">×</button>
      </div>
      <div class="image-viewer-body">
        <div v-if="loading" class="loading">
          <div class="loading-spinner"></div>
          <p>加载中...</p>
        </div>
        <div v-else-if="imageUrl" class="image-container">
          <img :src="imageUrl" :alt="title" @error="handleImageError" />
        </div>
        <div v-else class="no-image">
          <div class="no-image-icon">📄</div>
          <p>{{ noImageMessage }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'

interface Props {
  visible: boolean
  title: string
  imageUrl?: string
  noImageMessage?: string
}

const props = withDefaults(defineProps<Props>(), {
  noImageMessage: '暂无图片'
})

const emit = defineEmits<{
  close: []
}>()

const loading = ref(false)

// 监听visible变化，当打开时开始加载
watch(() => props.visible, (newVal) => {
  if (newVal && props.imageUrl) {
    loading.value = true
    // 模拟图片加载
    setTimeout(() => {
      loading.value = false
    }, 500)
  }
})

const closeViewer = () => {
  emit('close')
}

const handleImageError = () => {
  console.error('图片加载失败:', props.imageUrl)
}
</script>

<style scoped>
.image-viewer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-viewer-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
}

.image-viewer-content {
  position: relative;
  background: white;
  border-radius: 8px;
  max-width: 90%;
  max-height: 90%;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.image-viewer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 20px;
  border-bottom: 1px solid #e8e8e8;
  background-color: #f8f9fa;
}

.image-viewer-header h3 {
  margin: 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.close-button {
  background: none;
  border: none;
  font-size: 24px;
  color: #999;
  cursor: pointer;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.close-button:hover {
  background-color: #e8e8e8;
  color: #666;
}

.image-viewer-body {
  padding: 20px;
  max-height: 70vh;
  overflow-y: auto;
}

.loading {
  text-align: center;
  padding: 40px;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.image-container {
  text-align: center;
}

.image-container img {
  max-width: 100%;
  max-height: 60vh;
  object-fit: contain;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.no-image {
  text-align: center;
  padding: 40px;
  color: #999;
}

.no-image-icon {
  font-size: 48px;
  margin-bottom: 15px;
}

.no-image p {
  margin: 0;
  font-size: 14px;
}

@media (max-width: 768px) {
  .image-viewer-content {
    max-width: 95%;
    max-height: 95%;
  }
  
  .image-viewer-header {
    padding: 10px 15px;
  }
  
  .image-viewer-body {
    padding: 15px;
  }
}
</style> 